<?php
/**
 * 定期检测证书状态 以及增加新证书
 *
 */
class checkcerk extends exec
{
    function __construct()
    {
        parent::__construct();
        $this->loadModel('g5_certificate');
        $this->loadModel('g5_perfile');
        $this->loadModel('g5_cron_job');
        $this->loadModel('g5_original_pack');
    }

    /**
     * 重签计划任务
     * @author Giles <giles.wang@icloud.com>
     * @return void
     */
    function sign()
    {
       $url = "http://192.168.1.157/login/sign";
        file_get_contents($url);
    }

    /**
     * 检测证书是否被封 以及新证书添加
     */
    function checkCer()
    {
        $url = "http://192.168.1.157/login/checkCer";
        file_get_contents($url);
    }

    /**
     * 检测证书是否存在 如果存在检测状态
     * @param $cerk
     * @param $status
     * @author Giles <giles.wang@icloud.com>
     * @return boole
     *
     */
    function ischeck($cerk, $status)
    {

        $res = $this->g5_certificate->getOne('id,name, md5, status', array('name' => $cerk));
        if(empty($res)) {
            return true;
        }
        if($res['status'] == cert::CLOSEDOWN) {
            return false;
        }

        if($status != $res['status'] && $status == cert::CLOSEDOWN) {
            $time = time();
            $up = array('status' => cert::CLOSEDOWN, 'lastCheckTime' => $time, 'killTime' => $time);
            $this->g5_certificate->update($up, array('id' => $res['id']));
        }

        if($status == $res['status']) {
            $time = time();
            $up = array('lastCheckTime' => $time);
            $this->g5_certificate->update($up, array('id' => $res['id']));
        }

        return false;
    }


    /**
     * 获取解压后的包路径
     * @author Giles <giles.wang@icloud.com>
     * @return String
     */
    function getDir($dir) {
        $dirArray[]=NULL;
        if (false != ($handle = opendir ($dir))) {
            $i=0;
            while ( false !== ($file = readdir($handle))) {
                //去掉"“.”、“..”以及带“.xxx”后缀的文件
                if (strpos($file, '.app')) {
                    $dirArray[$i]=$file;
                    $i++;
                }
            }
            //关闭句柄
            closedir ($handle);
        }
        $lastDir = $dir . '/'. $dirArray[0].'/';
        if(strpos($lastDir, ' ')) {
            $lastDir = str_replace(' ', '\ ', $lastDir);
        }
        return $lastDir;
    }

}